<template>
  <div class="app-container">
    <query :query-params="queryParams" @getList="getList" @handleRegister="handleRegister"/>
    <customize-table ref="registerTable" :query-params="queryParams"/>
    <register-form ref="disposalForm" :disposal-stat="disposalStat" :pro-list="proList" @handSuccess="handSuccess"/>
  </div>
</template>
<script>
import {
  batchRegister,
  getTask
} from '@/api/oms/dealTask'
import Query from './query'
import CustomizeTable from './table'
import { uniqueArr } from '@/utils'
import RegisterForm from '@/views/wms/deal/register/form'

export default {
  name: 'Register',
  dicts: ['wms_deal_register', 'deal_type'],
  components: { RegisterForm, Query, CustomizeTable },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        authCenter: '1',
        region: 'SX'
      },
      disposalStat:undefined,
      proList:[],
    }
  },
  methods: {
    getList(){
      this.$refs.registerTable.getList()
    },
    handleView(id) {
      this.loading = true
      this.reset()
      getTask(id).then(response => {
        let form = response.data
        this.detail = response.data
        if (form.registerInfo) {
          this.form = JSON.parse(form.registerInfo)
          this.form.oss = response.data.oss
        } else {
          this.form.disposalStat = form.dealType
          this.form.disposalNumber = form.nums
        }
        this.loading = false
        this.showDetail = true
      })
    },
    close() {
      this.reset()
      this.showDetail = false
    },
    handSuccess(){
      this.disposalStat = undefined;
      this.getList()
    },
    handleRegister(){
      const selectList = this.$refs.registerTable.selectList
     if(selectList && selectList.length){
       const uniqueDisposalStat = uniqueArr(selectList.map(item=>item.disposalType))
       if(uniqueDisposalStat && uniqueDisposalStat.length ===1){
         this.disposalStat = uniqueDisposalStat.pop()
         this.proList = selectList
         this.$refs.disposalForm.showRegister = true
       }else {
         this.$message.error("请选择一种处置意见")
       }
     }else {
       this.$message.error("请选择物品")
     }
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          let registerInfo = JSON.stringify(this.form)
          this.obj.registerInfo = registerInfo
          batchRegister(this.obj).then(response => {
            this.$modal.msgSuccess('登记成功')
            this.show = false
            this.getList()
          }).finally(() => {
            this.buttonLoading = false
          })
        }
      })
    },
    getPdfName(fileUrl) {
      let files = fileUrl.split('/')
      let _names = files[files.length - 1]
      let fileName = _names
      if (_names && _names.indexOf('_') != -1) {
        let fileNames = _names.split('_')
        fileName = fileNames[fileNames.length - 1]
      }
      return fileName
    },
    getPdfUrl(fileUrl) {
      let markParams = {
        'text': '三明市安保信息科技有限公司',
        'fontSize': '20',
        'color': 'rgba(128,128,128,0.6)',
        'rotate': '330',
        'Place': 'middleCenter',
        'Rule': 'all',
        'tiling': 'false',
        'fontName': 'SimSun',
        'distanceX': '10',
        'distanceY': '10'
      }
      let host = location.hostname
      if (host == '192.168.1.256') {
        host = host.replace('192.168.1.256', '192.168.1.241')
      }
      if (host == '192.168.1.246') {
        host = host.replace('192.168.1.246', '192.168.1.241')
      }
      if (host == '10.49.241.156') {
        host = host.replace('10.49.241.156', '10.49.241.151')
      }
      let origin = location.protocol + '//' + host
      let url = origin + ':8095/reader?file=' + fileUrl + '&markParams=' + encodeURIComponent(JSON.stringify(markParams))
      return url
    }
  }
}
</script>
<style>

</style>
